package com.tcs.monitoring.task.impl;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tcs.model.monitoring.info.AbstractMonitoringInfo;
import com.tcs.model.monitoring.info.MonitoringNetInfo;
import com.tcs.monitoring.task.AbstractMonitoring;
import com.tcs.util.constant.SystemConstants;
import com.tcs.zookeeper.bean.CuratorBean;

/**
 * 此版本修改过.
 * @version 0.1
 * @author wangBo
 */
public class MonitoringNetServersImpl extends AbstractMonitoring {

	private final Logger logger = LoggerFactory.getLogger(MonitoringNetServersImpl.class);

	// 不再使用硬编码的方式.

	private CuratorBean curatorBean;
	
	private AbstractMonitoringInfo abstractMonitoringInfo = new MonitoringNetInfo();
	
	private ReentrantLock lock = new ReentrantLock();

	private PathChildrenCache cache;

	@Override
	public void monitoring() {
//		boolean isPing = false;
//		for (String ip : this.ipArray) {
//			try {
//				isPing = Ping.ping(ip, 5);
//				this.logger.info(" ip: {} , 是否ping 通 : {}", ip, Boolean.valueOf(isPing));
//				if (!isPing) {
//					this.abstractMonitoringInfo.setSubject("网络集群扫描引擎扫描出警告 : " + ip + " 节点网络掉线!").setContent(
//							"================================================================= \n网络集群扫描引擎扫描出警告 : " + ip
//									+ " 节点网络掉线,请检查服务器是否挂掉!\n" + "如果在短时间内未能收到告警.将确认服务器挂掉, \n" + "请重启服务器!");
//					this.iSendService.sendObject(this.abstractMonitoringInfo);
//				}
//			} catch (IOException e) {
//				this.logger.error("", e);
//			}
//		}
		// 使用zk来监控,不再使用硬编码的方式.
		lock.lock();
		Condition condition = lock.newCondition();
		
		try {
			CuratorFramework cf = curatorBean.getCuratorFrameworkAndStart();
			Stat stat = cf.checkExists().forPath(SystemConstants.MontioringConstants.ZKNODEPATH);
			if (null == stat) {
				cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(SystemConstants.MontioringConstants.ZKNODEPATH);
			}
			logger.info("--ip : {} " , monitoringProperty.getIp());
			cf.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(SystemConstants.MontioringConstants.ZKNODEPATH + "/" + monitoringProperty.getIp());
			cache = new PathChildrenCache(cf, SystemConstants.MontioringConstants.ZKNODEPATH, true);
			cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);			// 初始化监听
			cache.getListenable().addListener(new PathChildrenCacheListener() {
				
				@Override
				public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
					switch (event.getType()) {
					case CHILD_ADDED:
						try {
							abstractMonitoringInfo.setSubject("集群机器,新增了一台机器 " + event.getData().getPath()).setContent(event.getData().getStat().toString());
							logger.info("/-新增了 : {} " , abstractMonitoringInfo);
							iSendService.sendObject(abstractMonitoringInfo , 1 , SystemConstants.MontioringConstants.NET);
						} catch (Exception e) {
							logger.error("", e);
						}
						break;
					case CHILD_REMOVED:
						try {
							abstractMonitoringInfo.setSubject("集群机器,挂掉了一台机器 " + event.getData().getPath()).setContent(event.getData().getStat().toString());
							logger.info("/-移除了 : {} " , abstractMonitoringInfo);
							iSendService.sendObject(abstractMonitoringInfo , 1 , SystemConstants.MontioringConstants.NET);
						} catch (Exception e) {
							logger.error("", e);
						}
						break;
					default:
						break;
					}
				}
			});
			condition.await();
		} catch (Exception e) {
			logger.error("" , e);
		} finally {
			lock.unlock();
		}
	}

	public CuratorBean getCuratorBean() {
		return curatorBean;
	}

	public void setCuratorBean(CuratorBean curatorBean) {
		this.curatorBean = curatorBean;
	}
}